package com.system.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.system.entity.User;
import com.system.service.IUserService;
import com.system.service.UserServiceImpl;

public class UserChangePasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public UserChangePasswordServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String oldPassword = request.getParameter("oldPassword");
		
		User user = new User(username, oldPassword);
		IUserService userService = new UserServiceImpl();
		PrintWriter out = response.getWriter();
		if(userService.checkUser(user) == username) {
			if (userService.changePassword(username,password) == "1") {
				out.print("{\"code\":\"1\"}");
			}else {
				out.print("{\"code\":\"0\"}");
			}
		}else {
			out.print("{\"code\":\"0\"}");
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
